"use strict";

app.directive("ngDown", function () {
    return function ($scope, $element, $attributes) {
        var down = -1;
        down = false;
        $element.on("touchstart mousedown", function (event) {
            event.preventDefault();
            down = 0;
            callback();
        });
        $element.on("touchmove mousemove", function (event) {
            event.preventDefault();
            down = -1;
            return event.stopImmediatePropagation();
        });
        $element.on("touchend mouseup", function (event) {
            event.preventDefault();
            down = -1;
        });

        function callback() {
            if (down == -1) return;
            down++;
            $scope.$apply($attributes["ngDown"]);
            var delay = 150;
            if (down > 3) delay = 100;
            if (down > 9) delay = 70;
            if (down > 16) delay = 30;
            setTimeout(callback, delay);
        };
    };
});
